#include"OiPCH.hpp"

namespace Oi {
    namespace PLScannerDll {

        MyChartViewer::MyChartViewer(int range,QWidget* parent) : QWidget(parent)
        {
            _myView = new QChartView();
            _myView->setAttribute(Qt::WA_TransparentForMouseEvents, true);
            QHBoxLayout* mainLayout = new QHBoxLayout();
            mainLayout->addWidget(_myView);
            mainLayout->setContentsMargins(0, 0, 0, 0);
            this->setLayout(mainLayout);
            QPen penY(Qt::darkBlue, 8, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin);
            _chart = new QChart();
            _series1 = new QLineSeries;
            _series2 = new QLineSeries;
            _series3 = new QLineSeries;
            _seriesLine = new QLineSeries;
            _axisX = new QDateTimeAxis();
            _axisY = new QValueAxis();
            _stdLine1 = new QLineSeries();
            _stdLine2 = new QLineSeries();
            _stdLine1->setPen(penY);
            _stdLine2->setPen(penY);
            _myView->setChart(_chart);
            _chart->addSeries(_series1);
            _chart->addSeries(_series2);
            _chart->addSeries(_series3);
//            _chart->addSeries(_seriesLine);
            _chart->addSeries(_stdLine1);
            _chart->addSeries(_stdLine2);

            _chart->legend()->hide();                             //隐藏图例
            _axisX->setTickCount(20);                             //设置坐标轴格数
            _axisY->setTickCount(6);
            _axisX->setFormat("mm:ss");                        //设置时间显示格式
            _axisY->setMin(range - 1);                                    //设置Y轴范围
            _axisY->setMax(range + 1);
            _axisY->setRange(range-1, range+1);//设置坐标轴范围
            _axisX->setTitleText("date");                       //设置X轴名称
            //_axisY->setLinePenColor(QColor(Qt::red));        //设置坐标轴颜色样式
            //_axisY->setGridLineColor(QColor(Qt::red));
            _axisY->setGridLineVisible(false);                    //设置Y轴网格不显示
            _axisX->setGridLineVisible(false);                    //设置X轴网格不显示

            _chart->addAxis(_axisX, Qt::AlignBottom);               //设置坐标轴位于_chart中的位置
            _chart->addAxis(_axisY, Qt::AlignLeft);
            _series1->attachAxis(_axisX);
            _series1->attachAxis(_axisY);
            _series2->attachAxis(_axisX);
            _series2->attachAxis(_axisY);
            _series3->attachAxis(_axisX);
            _series3->attachAxis(_axisY);
            _seriesLine->attachAxis(_axisX);                           //把数据添加到坐标轴上
            _seriesLine->attachAxis(_axisY);
            _stdLine1->attachAxis(_axisX);                           //把数据添加到坐标轴上
            _stdLine1->attachAxis(_axisY);
            _stdLine2->attachAxis(_axisX);                           //把数据添加到坐标轴上
            _stdLine2->attachAxis(_axisY);
            _axisY->setTitleText("thickness");
            _myView->chart()->legend()->setFont(QFont("Arial", 19));
            _myView->chart()->legend()->setVisible(true);
            _myView->chart()->legend()->setAlignment(Qt::AlignTop);
    
            _myView->chart()->setMinimumHeight(300);
        }

        MyChartViewer::~MyChartViewer()
        {
            if (_myView)
            {
                delete _myView;
                _myView = NULL;
            }
        }

        void MyChartViewer::setRange(double maxRange, double minRange)
        {
            _axisY->setMin(minRange - 0.08);                                    //设置Y轴范围
            _axisY->setMax(maxRange + 0.08);
            _axisY->setRange(minRange - 0.08, maxRange + 0.08);//设置坐标轴范围

        }

        void MyChartViewer::AddChart(int nCount)
        {
            _count = nCount;
            if (nCount == 1)
            {
                _chart->addSeries(_series1);
                _chart->addSeries(_seriesLine);
                _series1->attachAxis(_axisX);
                _series1->attachAxis(_axisY);
            }
            else if (nCount == 2)
            {
                _chart->addSeries(_series1);
                _chart->addSeries(_series2);
                _chart->addSeries(_seriesLine);
                _series1->attachAxis(_axisX);                           
                _series1->attachAxis(_axisY);
                _series2->attachAxis(_axisX);
                _series2->attachAxis(_axisY);
            }
            else if (nCount == 3)
            {
                _chart->addSeries(_series1);
                _chart->addSeries(_series2);

                _chart->addSeries(_seriesLine);
                _series1->attachAxis(_axisX);
                _series1->attachAxis(_axisY);
                _series2->attachAxis(_axisX);
                _series2->attachAxis(_axisY);
            }


        }
        //画数据、动态更新数据
        void MyChartViewer::DrawLine(std::vector<double> vecPos, double stdValue, double errand)
        {
            _series1->setPen(QPen(Qt::red, 2, Qt::SolidLine));
            if (_series2)
            {
                _series2->setPen(QPen(Qt::yellow, 2, Qt::SolidLine));
            }
           
            _stdLine1->setPen(QPen(Qt::darkCyan, 2, Qt::DashLine));
            _stdLine2->setPen(QPen(Qt::darkCyan, 2, Qt::DashLine));
            QDateTime currentTime = QDateTime::currentDateTime();
            //设置坐标轴显示范围
            _chart->axisX()->setMin(QDateTime::currentDateTime().addSecs(-60 * 3));       //系统当前时间的前一秒
            _chart->axisX()->setMax(QDateTime::currentDateTime().addSecs(0));            //系统当前时间
            
            //增加新的点到曲线末端
            if (vecPos.size() > 0 && _series1)
            {
                _series1->append(currentTime.toMSecsSinceEpoch(), vecPos[0]);
            }
            if (vecPos.size() > 1 && _series2)
            {
                _series2->append(currentTime.toMSecsSinceEpoch(), vecPos[1]);
            }
            if (vecPos.size() > 2 && _series3)
            {
                _series3->append(currentTime.toMSecsSinceEpoch(), vecPos[2]);
            }
            _seriesLine->append(currentTime.toMSecsSinceEpoch(), 0);
            _stdLine1->append(currentTime.toMSecsSinceEpoch(), stdValue + errand);
            _stdLine2->append(currentTime.toMSecsSinceEpoch(), stdValue - errand);
        }

        void MyChartViewer::setDefaultName(QString name)
        {
            if (_count == 1)
            {
                _series1->setName("width");
            }
            else if (_count == 2)
            {
                _series1->setName("m1");
                _series2->setName("m5");
            }
            else if (_count == 3)
            {
                _series1->setName("m2");
                _series2->setName("m3");
                _series3->setName("m4");
            }
        }

        void MyChartViewer::setTheme(QChart::ChartTheme theme)
        {
            _myView->chart()->setTheme(theme);
            _myView->chart()->legend()->setFont(QFont("Arial", 19));
        }
        void MyChartViewer::clearViewer()
        {
            if (_series1)
            {
                _series1->clear();
            }

            if (_series2)
            {
                _series2->clear();
            }

            if (_series3)
            {
                _series3->clear();
            }
        }
       
    }
}
